import { CDStage } from "../excel/CDStage";
import { CDStageMainChapter } from "../excel/CDStageMainChapter";
import { CEStage } from "../excel_extend/CEStage";
import { Player } from "../struct/Player";


/**
 * 数据-关卡
 */
export abstract class SDStage {


    /**
     * 是否开启
     * @param player 
     * @param id 
     * @returns 
     */
    public static isPointOpen(player: Player, id: number): boolean {
        return player.battleStage.battleStageMaxId >= id;
    }


    /**
     * 是否可设置为挂机
     * @param player 
     * @param id 
     */
    public static isCanHangeup(player: Player, id: number): boolean {
        return player.battleStage.battleStageMaxId >= id;
    }


    /**
    * 是否当前挂机
    * @param player 
    * @param id 
    * @returns 
    */
    public static isHangeUp(player: Player, id: number): boolean {
        return player.battleStage.hangUpStageId === id;
    }


    /**
     * 主线关卡序号
     * @param player 
     */
    public static getMainStageIndex(player: Player): number {
        const list = CEStage.chapterStage.getValues();
        const selStageID = player.battleStage.battleStageId;
        let idx = 0;

        for (let i = 0, len = list.length; i < len; i++) {
            const vs = list[i].getValues();
            for (let j = 0, len2 = vs.length; j < len2; j++) {
                const vs2 = vs[j];
                for (let k = 0, len3 = vs2.length; k < len3; k++) {
                    if (vs2[k] === selStageID) {
                        return idx;
                    }
                    idx++;
                }
            }
        }

        return 0;
    }


    /**
     * 当前章节（相当于难度）
     * @param player 
     * @returns 
     */
    public static getMainStageChapter(player: Player): number {
        const stageId = player.battleStage.battleStageId;
        const config = CDStage.getData(stageId);
        const mainChapterConfig = CDStageMainChapter.getData(config.chapter);
        return mainChapterConfig.preChapter + 1;
    }


    /**
     * 当前页序号（相当于难度）
     * @param player 
     * @returns 
     */
    public static getMainStagePageIndex(player: Player): number {
        const stageId = player.battleStage.battleStageId;
        const config = CDStage.getData(stageId);
        const mainChapterConfig = CDStageMainChapter.getData(config.chapter);
        const chapter = mainChapterConfig.preChapter + 1;
        const chapterMap = CEStage.chapterStage.get(chapter);
        const pageList = chapterMap.getValues();

        for (let i = 0, len = pageList.length; i < len; i++) {
            if (pageList[i].indexOf(stageId) >= 0) {
                return i;
            }
        }

        return -1;
    }





}
